﻿using DapperExtensions;
using KD.Service.HR.Utility;
using Logistics.Model;
using Logistics.Model.QueryModel;
using Logistics.Model.Response;
using Logistics.Service;
using Logistics.Utility;
using Logistics.WebApi.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using WMLogistics.Model;
using WMLogistics.Service;

namespace Logistics.WebApi.Controllers
{
    [MyAuthority]
    public class WMBidLoadController : ApiController
    {
        private WMBidBaseService bidbase = new WMBidBaseService();
        private WMBidLoadService service = new WMBidLoadService();
        private WMBidWLPriceService priceService = new WMBidWLPriceService();
        private AccountService accountService = new AccountService();
        private CompanyService companyService = new CompanyService();
        private AndroidTokenService androidService = new AndroidTokenService();
        private CompushBaseService compushService = new CompushBaseService();




        /// <summary>
        /// 手机端调用
        /// </summary>
        /// <param name="bidbase"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel GetBidLoadInfo(BidPhoneQuery bidbase)
        {
            ResponseModel resultmod = new ResponseModel();
            resultmod.InitResultState(GetDataStateEnum.Success);
            resultmod.mod = service.GetWebLoadInfo(bidbase.load_id);
            return resultmod;
        }


        /// <summary>
        /// 历史运单查询
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel GetPageList(BidLoadQuery query)
        {
            if (query == null)
                query = new BidLoadQuery();
            var resultmod = new ResponseModel();
            try
            {
                var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
                var company = companyService.GetCompanyByAccountId(accountId);
                var loadQuery = new List<IPredicate>();
                if (!string.IsNullOrEmpty(query.bid_no))
                    loadQuery.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.bid_no, Operator.Like, "%"+query.bid_no+"%"));
                if (!string.IsNullOrEmpty(query.orderNumber))
                    loadQuery.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.order_num, Operator.Like, "%"+query.orderNumber+"%"));
                if (query.load_state > 0)
                {
                    if (query.load_state == 200)
                    {
                        loadQuery.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.load_state, Operator.Ge, 20));
                        loadQuery.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.load_state, Operator.Le, 30));
                    }
                    else
                     {
                         loadQuery.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.load_state, Operator.Eq, query.load_state));
                     }
                }
                    
                else
                    loadQuery.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.load_state, Operator.Gt, 30));

                if (query.consignee_time1 > 0)
                    loadQuery.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.consignee_time, Operator.Ge, query.consignee_time1 / 1000));
                if (query.consignee_time2 > 0)
                    loadQuery.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.consignee_time, Operator.Le, query.consignee_time2 / 1000));
                loadQuery.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.company_no, Operator.Eq, company.company_no));

                //if (query.pickup_time1 >0)
                //{
                //    DateTime pickuptime1 = DateConvert.StampToDateTime((query.pickup_time1 / 1000).ToString());
                //    loadQuery.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.pickup_time, Operator.Ge,pickuptime1 ));
                //}
                   
                //if (query.pickup_time2 >0)
                //{
                //    DateTime pickuptime2 = DateConvert.StampToDateTime((query.pickup_time2 / 1000).ToString());
                //    loadQuery.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.pickup_time, Operator.Le, pickuptime2));
                //}
                var loadPredicate = Predicates.Group(GroupOperator.And, loadQuery.ToArray());
                var BidLoadList = service.GetBidLoadList(loadPredicate, query.page - 1, query.pagesize);
                resultmod.mod = BidLoadList;
                if (BidLoadList.TotalNum > 0)
                {
                    resultmod.InitResultState(GetDataStateEnum.Success);
                }
                else
                {
                    resultmod.InitResultState(GetDataStateEnum.Error01);
                }
            }
            catch (Exception ex)
            {
                resultmod.InitResultState(ex);
            }
            return resultmod;
        }

        /// <summary>
        /// 获取运单列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
    
        [HttpPost]
        public ResponseModel GetTranspotList(TransportQuery query)
        {
            //if (query == null)
            //    query = new BidLoadQuery();
            IBetweenPredicate prestate;
            var resultmod = new ResponseModel();
            try
            {
                var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
                var company = companyService.GetCompanyByAccountId(accountId);

                var list = new List<IPredicate>();

                if (query.consignee_time1 > 0)
                    list.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.consignee_time, Operator.Ge, query.consignee_time1 / 1000));
                if (query.consignee_time2 > 0)
                    list.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.consignee_time, Operator.Le, query.consignee_time2 / 1000));
                //if (!string.IsNullOrEmpty(query.bid_no))
                //    list.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.bid_no, Operator.Eq, query.bid_no));
                //if (!string.IsNullOrEmpty(query.orderNumber))
                //    list.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.order_num, Operator.Eq, query.orderNumber));
                list.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.company_no, Operator.Eq, company.company_no));
                if (query.state > 0)
                {
                    if(query.state==200)
                    {
                        list.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.load_state, Operator.Ge, 20));
                        list.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.load_state, Operator.Le, 30));
                    }
                    else
                    {
                        list.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.load_state, Operator.Eq, query.state));
                    }
                }
                    
                if (query.wuliu_bid_state > 0)
                    list.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.wuliu_bid_state, Operator.Eq, query.wuliu_bid_state));
                var pg = Predicates.Group(GroupOperator.And, list.ToArray());
                var BidLoadList = service.GetTranspotList(pg, query.page - 1, query.pagesize);
                resultmod.mod = BidLoadList;
                if (BidLoadList.TotalNum > 0)
                {
                    resultmod.InitResultState(GetDataStateEnum.Success);
                }
                else
                {
                    resultmod.InitResultState(GetDataStateEnum.Error01);
                }
            }
            catch (Exception ex)
            {
                resultmod.InitResultState(ex);
            }
            return resultmod;
        }


        /// <summary>
        /// 装货查看
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel GetLoadView(PageQuery page)
        {
            var resultmod = new ResponseModel();
            try
            {
                var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
                var company = companyService.GetCompanyByAccountId(accountId);

                //bid_flag='1' and (wuliu_bid_state='20' or wuliu_bid_state='10')  and (load_state ='1'  or load_state='10')
                var list = new List<IPredicate>();
                list.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.bid_flag, Operator.Eq, "1"));
                list.Add(Predicates.Group(GroupOperator.Or, Predicates.Field<wuliu_wm_bid_load>(f => f.wuliu_bid_state, Operator.Eq, "20"),
                    Predicates.Field<wuliu_wm_bid_load>(f => f.wuliu_bid_state, Operator.Eq, "10")));
                list.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.company_no, Operator.Eq, company.company_no));
                list.Add(Predicates.Group(GroupOperator.Or, Predicates.Field<wuliu_wm_bid_load>(f => f.load_state, Operator.Eq, "1"),
                    Predicates.Field<wuliu_wm_bid_load>(f => f.load_state, Operator.Eq, "10")));
                var pg = Predicates.Group(GroupOperator.And, list.ToArray());
                var BidLoadList = service.GetTranspotList(pg, page.page - 1, page.pagesize);
                resultmod.mod = BidLoadList;
                if (BidLoadList.TotalNum > 0)
                {
                    resultmod.InitResultState(GetDataStateEnum.Success);
                }
                else
                {
                    resultmod.InitResultState(GetDataStateEnum.Error01);
                }
            }
            catch (Exception ex)
            {

                resultmod.InitResultState(ex);
            }
            return resultmod;
        }

        /// <summary>
        /// 装货列表
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel GetLoadList(PageQuery page)
        {
            var resultmod = new ResponseModel();
            try
            {
                //bid_flag='1' and (wuliu_bid_state='10')  and (load_state ='1'  or load_state='10')
                var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
                var company = companyService.GetCompanyByAccountId(accountId);
                var list = new List<IPredicate>();
                list.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.bid_flag, Operator.Eq, "1"));
                list.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.company_no, Operator.Eq, company.company_no));
                list.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.wuliu_bid_state, Operator.Eq, "10"));
                list.Add(Predicates.Group(GroupOperator.Or, Predicates.Field<wuliu_wm_bid_load>(f => f.load_state, Operator.Eq, "1"),
                    Predicates.Field<wuliu_wm_bid_load>(f => f.load_state, Operator.Eq, "10")));
                var pg = Predicates.Group(GroupOperator.And, list.ToArray());
                var BidLoadList = service.GetTranspotList(pg, page.page - 1, page.pagesize);
                resultmod.mod = BidLoadList;
                if (BidLoadList.TotalNum > 0)
                {
                    resultmod.InitResultState(GetDataStateEnum.Success);
                }
                else
                {
                    resultmod.InitResultState(GetDataStateEnum.Error01);
                }
            }
            catch (Exception ex)
            {

                resultmod.InitResultState(ex);
            }
            return resultmod;
        }

         [HttpPost]
        /// <summary>
        /// 获取待办事记录数
        /// </summary>
        /// <returns></returns>
        public ResponseModel GetStayOfficeCount()
        {
            var resultmod = new ResponseModel();
            try
            {
                var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
                var company = companyService.GetCompanyByAccountId(accountId);

                var list = new List<IPredicate>();
                list.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.bid_flag, Operator.Eq, "1"));
                list.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.company_no, Operator.Eq, company.company_no));
                list.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.wuliu_bid_state, Operator.Eq, "10"));
                list.Add(Predicates.Group(GroupOperator.Or, Predicates.Field<wuliu_wm_bid_load>(f => f.load_state, Operator.Eq, "1"),
                    Predicates.Field<wuliu_wm_bid_load>(f => f.load_state, Operator.Eq, "10")));
                var pg = Predicates.Group(GroupOperator.And, list.ToArray());
               int count1= service.GetTranspotCount(pg);

               var list2 = new List<IPredicate>();
               //if (!string.IsNullOrEmpty(query.bid_no))
               //    list.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.bid_no, Operator.Eq, query.bid_no));
               //if (!string.IsNullOrEmpty(query.orderNumber))
               //    list.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.order_num, Operator.Eq, query.orderNumber));
               list2.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.company_no, Operator.Eq, company.company_no));
               list2.Add(Predicates.Field<wuliu_wm_bid_load>(f => f.load_state, Operator.Eq, 20));
               var pg2 = Predicates.Group(GroupOperator.And, list2.ToArray());
               var count2 = service.GetTranspotCount(pg2);

                string strcount = count1 + "|" + count2;
                resultmod.mod = strcount;
                
                resultmod.InitResultState(GetDataStateEnum.Success);
            }
           catch (Exception ex)
            {
                resultmod.InitResultState(ex);
            }
            return resultmod;
        }

        /// <summary>
        /// 更新备注
        /// </summary>
        /// <param name="load"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel UpdateRemark(wuliu_wm_bid_load_query load)
        {
            var resultmod = new ResponseModel();
            resultmod.InitResultState(StateEnum.Success);
            try
            {
                resultmod.mod = service.UpdateRemark(load.id, load.load_remark);
                resultmod.InitResultState(StateEnum.Success);
            }
            catch (Exception ex)
            {
                resultmod.InitResultState(ex);
            }
            return resultmod;
        }

        /// <summary>
        /// 已取货
        /// </summary>
        /// <param name="load_no"></param>
        /// <returns></returns>
        [HttpGet]
        public ResponseModel BillPick(string load_no, int bid_id)
        {
            ResponseModel resultmod = new ResponseModel();
            resultmod.InitResultState(StateEnum.Success);
            try
            {
                var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
                var company = companyService.GetCompanyByAccountId(accountId);
                string companyname = "";
                if(company!=null)
                {
                    companyname = company.contacts;
                }
                resultmod.mod = service.BillPick(load_no, companyname);
                resultmod.InitResultState(StateEnum.Success);
            }
            catch (Exception ex)
            {
                resultmod.InitResultState(ex);
            }
            return resultmod;
        }

        /// <summary>
        /// 关闭运单
        /// </summary>
        /// <param name="close"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel ClsoeBid(wuliu_wm_bid_close_query close)
        {
            var resultmod = new ResponseModel();
            resultmod.InitResultState(StateEnum.Success);
            try
            {
                resultmod.mod = service.ClsoeBid(close);

                resultmod.InitResultState(StateEnum.Success);
            }
            catch (Exception ex)
            {
                resultmod.InitResultState(ex);
            }
            return resultmod;
        }

        /// <summary>
        /// 第一次未取货
        /// </summary>
        /// <param name="close"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel NotPickUp(wuliu_wm_bid_close_query close)
        {
            var resultmod = new ResponseModel();
            resultmod.InitResultState(StateEnum.Error);
            try
            {
                var price_id = Convert.ToInt32(HttpContext.Current.Request.QueryString["price_id"]);
                var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
                var company = companyService.GetCompanyByAccountId(accountId);
                var load = service.GetLoad(close.load_no);
                var price = priceService.GetPriceBid(price_id);
                var bidmod = bidbase.GetBid((int)load.bid_id);
                var newLoad = new wuliu_wm_bid_load();
                newLoad.load_no = NoBuilder.GetLoadNo();
                newLoad.load_remark = "";
                newLoad.load_state = "1";//
                newLoad.load_time = DateTime.Now;
                newLoad.Operator = accountId.ToString();
                newLoad.order_num = load.order_num;
                newLoad.price = price.price;
                newLoad.tax1 = price.tax1;
                newLoad.tax_price1 = price.tax_price1;
                newLoad.receipt_time = load.receipt_time;
                newLoad.start_address = load.start_address;
                newLoad.start_bid_time = load.start_bid_time;
                newLoad.start_city_id = load.start_city_id;
                newLoad.start_city_name = load.start_city_name;
                newLoad.wuliu_id = price.wuliu_id;
                newLoad.wuliu_name = price.wuliu_name;
                newLoad.wuliu_no = price.wuliu_no;
                newLoad.wuliu_bid_state = "1";
                newLoad.bid_flag = "1";
                newLoad.bid_id = load.bid_id;
                newLoad.bid_no = load.bid_no;
                newLoad.bid_state = 10;//
                newLoad.company_name = load.company_name;
                newLoad.company_no = load.company_no;
                newLoad.consignee_time = load.consignee_time;
                newLoad.end_address = load.end_address;

                newLoad.end_bid_time = load.end_bid_time;

                var en = service.NotPickUp(close, price_id, price,newLoad);

                wuliu_bid_load_log bidloadlog = new wuliu_bid_load_log();
                bidloadlog.bid_id = load.bid_id;
                bidloadlog.bid_no = load.bid_no;
                bidloadlog.load_no = load.load_no;
                bidloadlog.wuliu_id = load.wuliu_id;
                bidloadlog.wuliu_name = load.wuliu_name;
                bidloadlog.wuliu_no = load.wuliu_no;
                bidloadlog.remark = price.wuliu_name + "未能来取货";
                bidloadlog.Operator = company.contacts;
                bidloadlog.operator_time = DateTime.Now;
                service.AddBidLoad(bidloadlog);

                resultmod.mod = en;

                #region 手机推送

                string title = "", content = "", registrationid = "";


                var list = androidService.GetAndroidToken((int)price.wuliu_id);

                foreach (var mod in list)
                {
                    try
                    {

                        content = bidmod.bid_no + "&&5&&尊敬的企运宝用户，恭喜您，您参与订单号为" + bidmod.bid_no + "的订单竞标成功！请尽快和发货方取得联系并安排出车，祝您一帆风顺。";

                        wuliu_compush_base compushmod = new wuliu_compush_base();
                        compushmod.account_id = mod.account_id;
                        compushmod.bid_id = bidmod.bid_id;
                        //string pushcontent = bidmod.bid_no + "&&" + bidmod.state + "&&请" + pick.ToString("hh:mm:00") + "到" + bidmod.end_city_name + "的" + bidmod.company_name + "取货，剩余取货时间1小时，订单号" + bidmod.bid_no + "，请尽快安排出车，祝您一帆风顺。";
                        compushmod.content = content;
                        compushmod.state = 1;
                        compushmod.type = 10;
                        compushmod.create_time = DateConvert.DateTimeToStamp(DateTime.Now);
                        compushmod.push_time = DateConvert.DateTimeToStamp(DateTime.Now.AddMinutes(1));//推送时间需要计算保存

                        var pushid = compushService.AddCompushBase(compushmod);

                        wuliu_compush_base_momory compushbase = new wuliu_compush_base_momory();
                        compushbase.push_id = pushid;
                        compushbase.account_id = mod.account_id;
                        compushbase.state = 1;
                        compushbase.type = 10;
                        compushbase.content = content;
                        var momoryid = compushService.AddCompushBaseMomory(compushbase);



                        registrationid = mod.android_token;
                        //PushAndroid(title, content, bidmod.bid_no, registrationid);//激光推送



                        //Thread.Sleep(1000);
                        wuliu_compush_base compushmods = new wuliu_compush_base();
                        compushmods.account_id = mod.account_id;
                        compushmods.bid_id = bidmod.bid_id;
                        string pushcontent = bidmod.bid_no + "&&4&&请" + DateConvert.StampToDateTime(bidmod.consignee_time.ToString()).ToString("hh:mm:00") + "到" + bidmod.start_address + "的" + bidmod.company_name + "取货，剩余取货时间1小时，订单号" + bidmod.bid_no + "，请尽快安排出车，祝您一帆风顺。";
                        compushmods.content = pushcontent;
                        compushmods.state = 1;
                        compushmods.type = 10;
                        compushmods.create_time = DateConvert.DateTimeToStamp(DateTime.Now);
                        compushmods.push_time = DateConvert.DateTimeToStamp(DateConvert.StampToDateTime(bidmod.consignee_time.ToString()).AddHours(-1));//推送时间需要计算保存

                        var pushids = compushService.AddCompushBase(compushmods);

                        wuliu_compush_base_momory compushbases = new wuliu_compush_base_momory();
                        compushbases.push_id = pushids;
                        compushbases.account_id = mod.account_id;
                        compushbases.state = 1;
                        compushbases.type = 10;
                        compushbases.content = pushcontent;
                        var momoryids = compushService.AddCompushBaseMomory(compushbases);

                        wuliu_compush_base compushmod2 = new wuliu_compush_base();
                        compushmod2.account_id = mod.account_id;
                        compushmod2.bid_id = bidmod.bid_id;
                        string pushcontent2 = bidmod.bid_no + "&&4&&请" + DateConvert.StampToDateTime(bidmod.consignee_time.ToString()).ToString("hh:mm:00") + "到" + bidmod.start_address + "的" + bidmod.company_name + "取货，剩余取货时间半小时，订单号" + bidmod.bid_no + "，请尽快安排出车，祝您一帆风顺。";
                        compushmod2.content = pushcontent2;
                        compushmod2.state = 1;
                        compushmod2.type = 10;
                        compushmod2.create_time = DateConvert.DateTimeToStamp(DateTime.Now);
                        compushmod2.push_time = DateConvert.DateTimeToStamp(DateConvert.StampToDateTime(bidmod.consignee_time.ToString()).AddHours(-0.5));//推送时间需要计算保存
                        var pushid2 = compushService.AddCompushBase(compushmod2);

                        wuliu_compush_base_momory compushbase2 = new wuliu_compush_base_momory();
                        compushbase2.push_id = pushid2;
                        compushbase2.account_id = mod.account_id;
                        compushbase2.state = 1;
                        compushbase2.type = 10;
                        compushbase2.content = pushcontent;
                        var momoryid2 = compushService.AddCompushBaseMomory(compushbase2);
                    }
                    catch (Exception ex)
                    {

                    }
                }

                var IOSlist = androidService.GetIOSToken((int)price.wuliu_id);

                foreach (var mod in IOSlist)
                {
                    try
                    {
                        content = bidmod.bid_no + "&&4&&尊敬的企运宝用户，恭喜您，您参与订单号为" + bidmod.bid_no + "的订单竞标成功！请尽快和发货方取得联系并安排出车，祝您一帆风顺。";

                        wuliu_compush_base compushmod = new wuliu_compush_base();
                        compushmod.account_id = mod.account_id;
                        compushmod.bid_id = bidmod.bid_id;
                        //string pushcontent = bidmod.bid_no + "&&" + bidmod.state + "&&请" + pick.ToString("hh:mm:00") + "到" + bidmod.end_city_name + "的" + bidmod.company_name + "取货，剩余取货时间1小时，订单号" + bidmod.bid_no + "，请尽快安排出车，祝您一帆风顺。";
                        compushmod.content = content;
                        compushmod.state = 1;
                        compushmod.type = 10;
                        compushmod.create_time = DateConvert.DateTimeToStamp(DateTime.Now);
                        compushmod.push_time = DateConvert.DateTimeToStamp(DateTime.Now.AddMinutes(1));//推送时间需要计算保存

                        var pushid = compushService.AddCompushBase(compushmod);

                        wuliu_compush_base_momory compushbase = new wuliu_compush_base_momory();
                        compushbase.push_id = pushid;
                        compushbase.account_id = mod.account_id;
                        compushbase.state = 1;
                        compushbase.type = 10;
                        compushbase.content = content;
                        var momoryid = compushService.AddCompushBaseMomory(compushbase);



                        registrationid = mod.ios_token;
                        //PushAndroid(title, content, bidmod.bid_no, registrationid);//激光推送

                        //Thread.Sleep(1000);
                        wuliu_compush_base compushmods = new wuliu_compush_base();
                        compushmods.account_id = mod.account_id;
                        compushmods.bid_id = bidmod.bid_id;
                        string pushcontent = bidmod.bid_no + "&&4&&请" + DateConvert.StampToDateTime(bidmod.consignee_time.ToString()).ToString("hh:mm:00") + "到" + bidmod.start_address + "的" + bidmod.company_name + "取货，剩余取货时间1小时，订单号" + bidmod.bid_no + "，请尽快安排出车，祝您一帆风顺。";
                        compushmods.content = pushcontent;
                        compushmods.state = 1;
                        compushmods.type = 10;
                        compushmods.create_time = DateConvert.DateTimeToStamp(DateTime.Now);
                        compushmods.push_time = DateConvert.DateTimeToStamp(DateConvert.StampToDateTime(bidmod.consignee_time.ToString()).AddHours(-1));//推送时间需要计算保存

                        var pushids = compushService.AddCompushBase(compushmods);

                        wuliu_compush_base_momory compushbases = new wuliu_compush_base_momory();
                        compushbases.push_id = pushids;
                        compushbases.account_id = mod.account_id;
                        compushbases.state = 1;
                        compushbases.type = 10;
                        compushbases.content = pushcontent;
                        var momoryids = compushService.AddCompushBaseMomory(compushbases);

                        wuliu_compush_base compushmod2 = new wuliu_compush_base();
                        compushmod2.account_id = mod.account_id;
                        compushmod2.bid_id = bidmod.bid_id;
                        string pushcontent2 = bidmod.bid_no + "&&4&&请" + DateConvert.StampToDateTime(bidmod.consignee_time.ToString()).ToString("hh:mm:00") + "到" + bidmod.start_address + "的" + bidmod.company_name + "取货，剩余取货时间半小时，订单号" + bidmod.bid_no + "，请尽快安排出车，祝您一帆风顺。";
                        compushmod2.content = pushcontent2;
                        compushmod2.state = 1;
                        compushmod2.type = 10;
                        compushmod2.create_time = DateConvert.DateTimeToStamp(DateTime.Now);
                        compushmod2.push_time = DateConvert.DateTimeToStamp(DateConvert.StampToDateTime(bidmod.consignee_time.ToString()).AddHours(-0.5));//推送时间需要计算保存
                        var pushid2 = compushService.AddCompushBase(compushmod2);

                        wuliu_compush_base_momory compushbase2 = new wuliu_compush_base_momory();
                        compushbase2.push_id = pushid2;
                        compushbase2.account_id = mod.account_id;
                        compushbase2.state = 1;
                        compushbase2.type = 10;
                        compushbase2.content = pushcontent;
                        var momoryid2 = compushService.AddCompushBaseMomory(compushbase2);
                    }
                    catch (Exception ex)
                    {

                    }


                }

                #endregion
                    
                

                //priceService.ChoiceBidPirce((int)close.bid_id, price_id);
                resultmod.InitResultState(StateEnum.Success);
            }
            catch (FormatException ex)
            {
                var e = new Exception("未重新选标", ex);
                resultmod.InitResultState(e);
            }
            catch (Exception ex)
            {
                resultmod.InitResultState(ex);
            }
            return resultmod;
        }

        /// <summary>
        /// 重新未取货重新选标
        /// </summary>
        /// <param name="load"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel ReNotPickUp(wuliu_wm_bid_load_query load)
        {
            var resultmod = new ResponseModel();
            resultmod.InitResultState(StateEnum.Error);
            try
            {
                var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
                var price_id = Convert.ToInt32(load.wuliu_id);//wuliu_id作为报价表的主键ID
                var price = priceService.GetPriceBid((int)load.wuliu_id);//wuliu_id作为报价表的主键ID
                var bidmod = bidbase.GetBid((int)load.bid_id);
                var newLoad = new wuliu_wm_bid_load();
                newLoad.load_no = NoBuilder.GetLoadNo();
                newLoad.load_remark = "";
                newLoad.load_state = "1";//
                newLoad.load_time = DateTime.Now;
                newLoad.Operator = accountId.ToString();
                newLoad.order_num = bidmod.number;
                newLoad.price = price.price;
                newLoad.tax_price1 = price.tax_price1;
                newLoad.tax1 = price.tax1;
                newLoad.tax2 = price.tax2;
                newLoad.receipt_time = (int)bidmod.receipt_time;
                newLoad.start_address = bidmod.start_address;
                newLoad.start_bid_time = (int)bidmod.start_bid_time;
                newLoad.start_city_id = bidmod.start_city_id;
                newLoad.start_city_name = bidmod.start_city_name;
                newLoad.wuliu_id = price.wuliu_id;
                newLoad.wuliu_name = price.wuliu_name;
                newLoad.wuliu_no = price.wuliu_no;
                newLoad.wuliu_bid_state = "1";
                newLoad.bid_flag = "1";
                newLoad.bid_id = bidmod.bid_id;
                newLoad.bid_no = bidmod.bid_no;
                newLoad.bid_state = 10;//
                newLoad.company_name = bidmod.company_name;
                newLoad.company_no = bidmod.company_no;
                newLoad.consignee_time = (int)bidmod.consignee_time;
                newLoad.end_address = bidmod.end_address;
                newLoad.end_bid_time = (int)bidmod.end_bid_time;

                var en= service.ReNotPickUp(load, price_id,price, newLoad);

                resultmod.mod = en;

                #region 手机推送

                    string title = "", content = "", registrationid = "";


                    var list = androidService.GetAndroidToken((int)price.wuliu_id);

                    foreach (var mod in list)
                    {
                        try
                        {

                            content = bidmod.bid_no + "&&4&&尊敬的企运宝用户，恭喜您，您参与订单号为" + bidmod.bid_no + "的订单竞标成功！请尽快和发货方取得联系并安排出车，祝您一帆风顺。";

                            wuliu_compush_base compushmod = new wuliu_compush_base();
                            compushmod.account_id = mod.account_id;
                            compushmod.bid_id = bidmod.bid_id;
                            //string pushcontent = bidmod.bid_no + "&&" + bidmod.state + "&&请" + pick.ToString("hh:mm:00") + "到" + bidmod.end_city_name + "的" + bidmod.company_name + "取货，剩余取货时间1小时，订单号" + bidmod.bid_no + "，请尽快安排出车，祝您一帆风顺。";
                            compushmod.content = content;
                            compushmod.state = 1;
                            compushmod.type = 10;
                            compushmod.create_time = DateConvert.DateTimeToStamp(DateTime.Now);
                            compushmod.push_time = DateConvert.DateTimeToStamp(DateTime.Now.AddMinutes(1));//推送时间需要计算保存

                            var pushid = compushService.AddCompushBase(compushmod);

                            wuliu_compush_base_momory compushbase = new wuliu_compush_base_momory();
                            compushbase.push_id = pushid;
                            compushbase.account_id = mod.account_id;
                            compushbase.state = 1;
                            compushbase.type = 10;
                            compushbase.content = content;
                            var momoryid = compushService.AddCompushBaseMomory(compushbase);



                            registrationid = mod.android_token;
                            //PushAndroid(title, content, bidmod.bid_no, registrationid);//激光推送



                            //Thread.Sleep(1000);
                            wuliu_compush_base compushmods = new wuliu_compush_base();
                            compushmods.account_id = mod.account_id;
                            compushmods.bid_id = bidmod.bid_id;
                            string pushcontent = bidmod.bid_no + "&&4&&请" + DateConvert.StampToDateTime(bidmod.consignee_time.ToString()).ToString("HH:mm:00") + "到" + bidmod.start_address + "的" + bidmod.end_address + "取货，剩余取货时间1小时，订单号" + bidmod.bid_no + "，请尽快安排出车，祝您一帆风顺。";
                            compushmods.content = pushcontent;
                            compushmods.state = 1;
                            compushmods.type = 10;
                            compushmods.create_time = DateConvert.DateTimeToStamp(DateTime.Now);
                            compushmods.push_time = DateConvert.DateTimeToStamp(DateConvert.StampToDateTime(bidmod.consignee_time.ToString()).AddHours(-1));//推送时间需要计算保存

                            var pushids = compushService.AddCompushBase(compushmods);

                            wuliu_compush_base_momory compushbases = new wuliu_compush_base_momory();
                            compushbases.push_id = pushids;
                            compushbases.account_id = mod.account_id;
                            compushbases.state = 1;
                            compushbases.type = 10;
                            compushbases.content = pushcontent;
                            var momoryids = compushService.AddCompushBaseMomory(compushbases);

                            wuliu_compush_base compushmod2 = new wuliu_compush_base();
                            compushmod2.account_id = mod.account_id;
                            compushmod2.bid_id = bidmod.bid_id;
                            string pushcontent2 = bidmod.bid_no + "&&4&&请" + DateConvert.StampToDateTime(bidmod.consignee_time.ToString()).ToString("HH:mm:00") + "到" + bidmod.start_address + "的" + bidmod.end_address + "取货，剩余取货时间半小时，订单号" + bidmod.bid_no + "，请尽快安排出车，祝您一帆风顺。";
                            compushmod2.content = pushcontent2;
                            compushmod2.state = 1;
                            compushmod2.type = 10;
                            compushmod2.create_time = DateConvert.DateTimeToStamp(DateTime.Now);
                            compushmod2.push_time = DateConvert.DateTimeToStamp(DateConvert.StampToDateTime(bidmod.consignee_time.ToString()).AddHours(-0.5));//推送时间需要计算保存
                            var pushid2 = compushService.AddCompushBase(compushmod2);

                            wuliu_compush_base_momory compushbase2 = new wuliu_compush_base_momory();
                            compushbase2.push_id = pushid2;
                            compushbase2.account_id = mod.account_id;
                            compushbase2.state = 1;
                            compushbase2.type = 10;
                            compushbase2.content = pushcontent;
                            var momoryid2 = compushService.AddCompushBaseMomory(compushbase2);
                        }
                        catch (Exception ex)
                        {

                        }
                    }

                    var IOSlist = androidService.GetIOSToken((int)price.wuliu_id);

                    foreach (var mod in IOSlist)
                    {
                        try
                        {
                            content = bidmod.bid_no + "&&4&&尊敬的企运宝用户，恭喜您，您参与订单号为" + bidmod.bid_no + "的订单竞标成功！请尽快和发货方取得联系并安排出车，祝您一帆风顺。";

                            wuliu_compush_base compushmod = new wuliu_compush_base();
                            compushmod.account_id = mod.account_id;
                            compushmod.bid_id = bidmod.bid_id;
                            //string pushcontent = bidmod.bid_no + "&&" + bidmod.state + "&&请" + pick.ToString("hh:mm:00") + "到" + bidmod.end_city_name + "的" + bidmod.company_name + "取货，剩余取货时间1小时，订单号" + bidmod.bid_no + "，请尽快安排出车，祝您一帆风顺。";
                            compushmod.content = content;
                            compushmod.state = 1;
                            compushmod.type = 10;
                            compushmod.create_time = DateConvert.DateTimeToStamp(DateTime.Now);
                            compushmod.push_time = DateConvert.DateTimeToStamp(DateTime.Now.AddMinutes(1));//推送时间需要计算保存

                            var pushid = compushService.AddCompushBase(compushmod);

                            wuliu_compush_base_momory compushbase = new wuliu_compush_base_momory();
                            compushbase.push_id = pushid;
                            compushbase.account_id = mod.account_id;
                            compushbase.state = 1;
                            compushbase.type = 10;
                            compushbase.content = content;
                            var momoryid = compushService.AddCompushBaseMomory(compushbase);



                            registrationid = mod.ios_token;
                            //PushAndroid(title, content, bidmod.bid_no, registrationid);//激光推送

                            //Thread.Sleep(1000);
                            wuliu_compush_base compushmods = new wuliu_compush_base();
                            compushmods.account_id = mod.account_id;
                            compushmods.bid_id = bidmod.bid_id;
                            string pushcontent = bidmod.bid_no + "&&4&&请" + DateConvert.StampToDateTime(bidmod.consignee_time.ToString()).ToString("HH:mm:00") + "到" + bidmod.start_address + "的" + bidmod.end_address + "取货，剩余取货时间1小时，订单号" + bidmod.bid_no + "，请尽快安排出车，祝您一帆风顺。";
                            compushmods.content = pushcontent;
                            compushmods.state = 1;
                            compushmods.type = 10;
                            compushmods.create_time = DateConvert.DateTimeToStamp(DateTime.Now);
                            compushmods.push_time = DateConvert.DateTimeToStamp(DateConvert.StampToDateTime(bidmod.consignee_time.ToString()).AddHours(-1));//推送时间需要计算保存

                            var pushids = compushService.AddCompushBase(compushmods);

                            wuliu_compush_base_momory compushbases = new wuliu_compush_base_momory();
                            compushbases.push_id = pushids;
                            compushbases.account_id = mod.account_id;
                            compushbases.state = 1;
                            compushbases.type = 10;
                            compushbases.content = pushcontent;
                            var momoryids = compushService.AddCompushBaseMomory(compushbases);

                            wuliu_compush_base compushmod2 = new wuliu_compush_base();
                            compushmod2.account_id = mod.account_id;
                            compushmod2.bid_id = bidmod.bid_id;
                            string pushcontent2 = bidmod.bid_no + "&&4&&请" + DateConvert.StampToDateTime(bidmod.consignee_time.ToString()).ToString("HH:mm:00") + "到" + bidmod.start_address + "的" + bidmod.end_address + "取货，剩余取货时间半小时，订单号" + bidmod.bid_no + "，请尽快安排出车，祝您一帆风顺。";
                            compushmod2.content = pushcontent2;
                            compushmod2.state = 1;
                            compushmod2.type = 10;
                            compushmod2.create_time = DateConvert.DateTimeToStamp(DateTime.Now);
                            compushmod2.push_time = DateConvert.DateTimeToStamp(DateConvert.StampToDateTime(bidmod.consignee_time.ToString()).AddHours(-0.5));//推送时间需要计算保存
                            var pushid2 = compushService.AddCompushBase(compushmod2);

                            wuliu_compush_base_momory compushbase2 = new wuliu_compush_base_momory();
                            compushbase2.push_id = pushid2;
                            compushbase2.account_id = mod.account_id;
                            compushbase2.state = 1;
                            compushbase2.type = 10;
                            compushbase2.content = pushcontent;
                            var momoryid2 = compushService.AddCompushBaseMomory(compushbase2);
                        }
                        catch (Exception ex)
                        {

                        }


                    }

                    #endregion
              
            }
            catch (Exception ex)
            {
                resultmod.InitResultState(ex);
            }
            return resultmod;
        }

        /// <summary>
        /// 回单确认
        /// </summary>
        /// <param name="pickback"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel Pickback(wuliu_bid_pickback_query pickback)
        {
            var resultmod = new ResponseModel();
            resultmod.InitResultState(StateEnum.Success);
            try
            {

                var result = service.Pickback(pickback);

                if (result)
                    resultmod.InitResultState(StateEnum.Success);
                else
                    resultmod.InitResultState(StateEnum.Error);
            }
            catch (FormatException ex)
            {
                var e = new Exception("未重新选标", ex);
                resultmod.InitResultState(e);
            }
            catch (Exception ex)
            {
                resultmod.InitResultState(ex);
            }
            return resultmod;
        }

        /// <summary>
        /// 获取运单详情
        /// </summary>
        /// <param name="bid_id"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel GetLoadDetail(int loadid)
        {
            //bidload
            var resultmod = new ResponseModel();
            resultmod.InitResultState(StateEnum.Success);
            try
            {
                var result = service.GetBidLoadDeltial(loadid);

            }
            catch (Exception ex)
            {
                resultmod.InitResultState(ex);
            }
            return resultmod;

        }


    }
}